<template>
<div>
    <h2 style="text-align: center;">用户借款</h2>
    <table border="1">
        <tr>
            <td>借款名称</td>
            <td>{{ info.B_Name }}</td>
        </tr>

        <tr>
            <td>借款最高金额</td>
            <td>{{ info.B_MaxMoney }}</td>
        </tr>

        <tr>
            <td>借款年限</td>
            <td>{{ info.B_Year }}</td>
        </tr>

        <tr>
            <td>利息</td>
            <td>{{ info.BI_Interest }}</td>
        </tr>

        <tr>
            <td>借款金额</td>
            <td>
                <input type="text" placeholder="请输入借款金额" v-model="UBMoney">
            </td>
        </tr>

        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="button" value="确认借款" @click="SaveData">
            </td>
        </tr>
    </table>    
</div>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue';
import {useRouter, useRoute} from 'vue-router';
import axios from 'axios';
import {useUserStore} from '../../store/index.ts'

//创建路由操作对象
const route=useRoute();
const router=useRouter();

//创建仓库实例
const useStore=useUserStore();

//创建响应式数据
const info=ref({
    B_Name:"",
    B_MaxMoney:0,
    B_Year:0,
    BI_Interest:0
})

const UBMoney=ref("")

//创建生命周期函数
onMounted(()=>{
    LoadData()
})

//完成数据加载功能
const LoadData=()=>{
    axios.get("https://localhost:44309/api/Borrows/ShowBidBorrow",
        {
            params:{
                Bid:route.params.Bid
            }
        }
    )
    .then(res=>{
        info.value=res.data[0]
    })
    .catch(error=>{
        console.log(error);
    })
}

//完成借款利息添加
const SaveData=()=>{
    //借款金额不能大于借款最高金额
    if(Number(UBMoney.value)>info.value.B_MaxMoney)
    {
        alert('借款金额不能大于借款最高金额');
        return;
    }

    axios.post("https://localhost:44309/api/Borrows/AddUserBorrow",
        {
            U_Id:useStore.Uid,
            B_Id:route.params.Bid,
            UB_Money:UBMoney.value
        }
    )
    .then(res=>{
        if(res.data>0)
        {
            alert('用户借款添加成功');
            router.push("/ShowAllUserBorrow")
        }
        else
        {
            alert('用户借款添加失败!!!');
        }
    })
    .catch(error=>{
        console.log(error)
    })
}

</script>